/**
 	FileDownload class
 */
package nrb.utils
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	
	import mx.controls.Alert;
	
	/**
	 	Type: Class
	 	@Params:none
	 	@return:none
	 	Desc: Class for File download
	 */
    public class FileDownload extends Sprite {
		
		/**
		 	variables for listener function and file reference
		 */
		private var file:FileReference;
		private var listener:Function;
		private var request:URLRequest;
		
		/**
		 	Type: Controller
		 	@Params:none
		 	@return:none
		 	Desc: Initializes variables and configure listeners for file
		 */
        public function FileDownload() {
            file = new FileReference();
            listener = new Function();
            request = new URLRequest();
            configureListeners(file);
        }
        
        /**
		 	Type: Public Function
		 	@Params:value['URL'] AS String
		 	@return:void
		 	Desc: sets url
		 */
        public function set_url(value:String): void {
        	request.url = value;
        }
        
        /**
		 	Type: Public Function
		 	@Params:obj[{listener:Function, url:String}] AS Object
		 	@return:void
		 	Desc: downloads file
		 */
        public function download_file(obj:Object): void {
        	listener = obj.listener;
        	//file.addEventListener(Event.COMPLETE, complete_handler);
        	file.download(request);
        	
        }
        
        /**
		 	Type: Private Function
		 	@Params:dispatcher[IEventDispatcher] AS IEventDispatcher
		 	@return:void
		 	Desc: adds event listeners to file variable
		 */
        private function configureListeners(dispatcher:IEventDispatcher):void {
            //dispatcher.addEventListener(Event.SELECT, select_handler);
		    dispatcher.addEventListener(Event.OPEN, open_handler);
		    dispatcher.addEventListener(ProgressEvent.PROGRESS, progress_handler);
		    dispatcher.addEventListener(Event.COMPLETE, complete_handler);
		    dispatcher.addEventListener(Event.CANCEL, cancel_handler);
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for select file
		 */
        private function select_handler(event:Event): void {
        	//obj_listener.text = event.target.name;
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for open file
		 */
        private function open_handler(event:Event): void {
        	
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for download progress
		 */
        private function progress_handler(event:ProgressEvent): void {
        	
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for cancel download
		 */
        private function cancel_handler(event:Event): void {
        	if(listener != null)
        		listener({flag:'cancel'});
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for complete download, calls listener function if not null
		 */
        private function complete_handler(event:Event): void {
        	if(listener != null)
        		listener({flag:'complete'});
        }
       

		
		
	}
}